<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class ShopController extends Controller
{
    public function index()
    {
    	// $data = DB::table('t_class as c1') -> select('*', DB::raw("concat(c1.path,',',c1.id) as path_str")) -> orderBy('path_str') -> get();
    	// dd($data -> all());
        $data = DB::table('t_class') -> where('pid', 0) -> get();
    	$shop = DB::table('t_class') -> whereIn('status',[1]) -> get();
    	// dd($data);
    	//获取数组长度来遍历
    	// $num = count($data);
    	// $shop = [];
    	// for($i =0; $i < $num; $i++)
    	// {
    	// 	$id = $data[$i] -> id;
    	// 	$shop[$i] = DB::table('t_class') -> where('pid', $id) -> get();
    	// }
		// dd($shop);		
        session(['publicData' => $data]);       
        session(['publicShop' => $shop]);
        // dd(session('publicShop'));
    	return view('welcome',['data' => $data, 'shop' => $shop]);
    }

    public function shopClass($id)
    {
        // dd($id);
        $data = DB::table('t_class') -> where('pid', 0) -> get();
        $shop = DB::table('t_class') -> get();
        // dd($shop);
        $shops = DB::table('t_goods') -> where('type_id', $id) -> whereIn('state', ['1']) -> get();
        $count = count($shops);
        $imgs = [];
        for($i=0;$i<$count;$i++)
        {
            $gid = $shops[$i] -> gid;
            $imgs[$i] = DB::table('t_photo') -> where('gid', $gid) -> whereIn('small',[1]) -> value('photo');
        }
        // dd($imgs);

        return view('home.shopClass.indexShop',['data' => $data, 'shop' => $shop, 'shops' => $shops, 'imgs' => $imgs]);
    }

    public function ajaxSearch(Request $request)
    {
        $data = $request -> all();
        $value = $data['value'];
        $sc = DB::table('t_goods') -> where('gname','like','%'.$value.'%') ->limit(3) -> get();
        // dd($sc);
        return $sc;
    }

    public function search(Request $requset)
    {
        $keyword = $requset -> all()['keyword'];
        $shop = DB::table('t_goods') -> where('gname', 'like', '%'.$keyword.'%') -> get();
        $num = count($shop);
        // dd($shop);
        for($i=0; $i<$num;$i++)
        {
            $gid = $shop[$i] -> gid;
            $imgs[$i] = DB::table('t_photo') -> where('gid', $gid) -> whereIn('small', [1]) -> value('photo');
        }
        // dd($imgs);
        return view('home.index.search',['shop' => $shop, 'imgs' => $imgs]);
    }

    

}
